package com.dzpk.gameserver.module.player;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.mina.core.session.IoSession;

/**
 * 在线玩家管理器
 * 
 * @author admin
 * 
 */
public class PlayerControler {

    // 角色ID为key
    private ConcurrentHashMap<Long, Player> roleIdMap = new ConcurrentHashMap<Long, Player>();

    // 角色名为key
    private ConcurrentHashMap<String, Player> roleNameMap = new ConcurrentHashMap<String, Player>();

    // 唯一ID为key
    private ConcurrentHashMap<Integer, Player> keyNumMap = new ConcurrentHashMap<Integer, Player>();

    // 用户ID为key
    private ConcurrentHashMap<Long, Player> userIdMap = new ConcurrentHashMap<Long, Player>();

    // 用户ID为key
    private ConcurrentHashMap<Long, IoSession> userSessionMap = new ConcurrentHashMap<Long, IoSession>();

    public void addUserSession(long userId, IoSession session) {
        userSessionMap.put(userId, session);
    }

    public boolean hasUserSession(long userId) {
        return userSessionMap.containsKey(userId);
    }

    public void removeUserSession(long userId) {
        userSessionMap.remove(userId);
    }

    public IoSession getUserSession(long userId) {
        return userSessionMap.get(userId);
    }

    private static PlayerControler playerControler = new PlayerControler();
    private  PlayerControler(){}

    public static PlayerControler getInstance(){
        return playerControler;
    }
    /**
     * 添加用户
     * 
     * @param roleId
     * @param player
     * @return
     */
    public Player put(long roleId, Player player) {
        return roleIdMap.put(roleId, player);
    }

    /**
     * 添加用户
     * 
     * @param userId
     * @param player
     */
    public void addUser(long userId, Player player) {
        userIdMap.put(userId, player);
    }


    public void addRoleNameMap(String roleName, Player player) {
        roleNameMap.put(roleName, player);
    }

    public void removeRoleNameMap(String roleName) {
        roleNameMap.remove(roleName);
    }

    /**
     * 根据角色ID读取用户信息
     * 
     * @param roleId
     * @return
     */
    public Player getPlayerByRoleId(long roleId) {
        return roleIdMap.get(roleId);
    }

    /**
     * 根据用户ID读取用户信息
     * 
     * @param userId
     * @return
     */
    public Player getPlayerByUserId(long userId) {
        return userIdMap.get(userId);
    }

    /**
     * 根据角色名读取用户信息
     * 
     * @param roleName
     * @return
     */
    public Player getPlayerByRoleName(String roleName) {
        return roleNameMap.get(roleName);
    }

    /**
     * 
     * @param keyNum
     * @return
     */
    public Player getPlayerByKeyNum(int keyNum) {
        return keyNumMap.get(keyNum);
    }

    /**
     * 移除一个用户
     * 
     * @param userId
     */
    public void removeUser(long userId) {
        userIdMap.remove(userId);
    }


    /**
     * 玩家是否在线
     * 
     * @param userId
     * @return
     */
    public boolean isUserOnline(long userId) {
        return userIdMap.containsKey(userId);
    }

    public boolean isKeyNumOnline(int keyNum) {
        return keyNumMap.containsKey(keyNum);
    }

    /**
     * 角色是否在线
     * 
     * @param roleId
     * @return
     */
    public boolean isRoleOnline(long roleId) {
        return roleIdMap.containsKey(roleId);
    }

    public Long getSessionId(long roleId) {
        if (getPlayerByRoleId(roleId) == null || getPlayerByRoleId(roleId).getSession() == null) {
            return null;
        } else {
            return getPlayerByRoleId(roleId).getSession().getId();
        }
    }

    public Collection<Player> getOnlinePlayers() {
        return roleIdMap.values();
    }
}
